/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package imageviewerdb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Kena
 */
public class DBInstance {

    public String framework = "embedded";
    public static String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    public static String protocol = "jdbc:derby:";
    private static DBInstance uniqueInstance;
    public Connection conn;    
    
    private DBInstance (Connection conn) { this.conn = conn; }
    
    public static DBInstance getDBInstance(){
        
        boolean notCreated = false;
        if (uniqueInstance == null){
            
            Connection con = null;
            
          try {   
               System.out.println("Check if DataBase exists..."); 
              
              Class.forName(driver).newInstance();
                       
              con = DriverManager.getConnection(protocol + "ImagesDB");
                          
            }
          // The DB has might not been created
            catch (Exception e){ System.out.println("Database does not exist. "); notCreated = true; }
                    
            if (notCreated){                                         
                try {                     
                           System.out.print("Trying to create...");
                           con = DriverManager.getConnection(protocol + "ImagesDB;create=true");
                    }
                        catch(SQLException t) {System.out.println("Error creating database. ");}                                        
            }                     
            
          if (con != null)  {
              
              createTables(con);
              uniqueInstance = new DBInstance(con);
          }            
        }
        return uniqueInstance;
    }
    
    private static boolean createTables(Connection con){   
        if (con == null) return false;
        
        try{
            
           Statement s = con.createStatement();
           //s.execute("drop table pictures");
           //s.execute("drop table favorites");                      
           // creating tables
            s.execute("create table pictures(path varchar(200) not null primary key, name varchar(100), extension varchar(3), description varchar(250), size varchar(50), topic varchar(50), confidentiality char(1) )");
            s.execute("create table favorites(path varchar(200) not null primary key constraint fk references pictures)");           
        }
        catch(SQLException e) { 
            System.out.println("Tables have already been created." ); 
            
        }        
        
        return true;
    }
    
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
    }

}
